#include "userdao.h"
#include <stdio.h>
//添加
void add(User *user)
{
    //先打开数据库，当成文件
    sqlite3 *db=NULL;
    sqlite3_open("/home/xhb/ronghua-ads/test.db",&db);
    //命令
    char sql[256];
    //替换
    sprintf(sql,"insert into t_user(id,name) values(%d,'%s')",user->id,user->name);

    //准备执行sql
    char *errorMsg;//错误信息
    sqlite3_exec(db,sql,NULL,NULL,&errorMsg);
    if (errorMsg!=NULL)
    { 
        printf("%s\n",errorMsg);
    }
    sqlite3_free(db);
}
//查找
void getOne(int id)
{
    //先打开数据库，当成文件
    sqlite3 *db=NULL;
    sqlite3_open("/home/xhb/ronghua-ads/test.db",&db);
    //命令
    //char sql[256];
    //替换
    //sprintf(sql,"insert into t_user(id,name) values(%d,'%s')",user->id,user->name);

   //准备执行sql
    char *errorMsg;//错误信息
    char** pResult;  
    int nRow;
    int nCol;
    sqlite3_get_table(db,"select * from t_user",&pResult,&nRow,&nCol,&errorMsg);
    if (errorMsg!=NULL)
    { 
        printf("%s\n",errorMsg);
    }
    printf("row=%d,col=%d,res=%s\n",nRow,nCol,pResult);
    int i, j;
            for(i = 0; i <= nRow; i++) {
                for(j = 0; j < nCol; j++) {
                    int temp=i * nCol + j;
                    printf("%s ", pResult[temp]);
                }
                printf("\n");
            } 
    //释放
    sqlite3_free(db);

}